package b1.controls.basic
{
	import b1.utils.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.text.*;
	import flash.ui.Mouse;

	public class BasicButton extends Sprite
	{
		public var userData:Object; 
		private var disableFlag:Boolean;
		
		public function BasicButton(BackgroundImage:Class, text:String = null, fontSize:int = 15, userData:Object = null)
		{
			super();
			//背景
			this.userData = userData;
			var backgroundImage:Sprite = new BackgroundImage();
			addChild(backgroundImage);
			if (text != null)
			{
				//标题名字
				var titleText:TextField = new TextField();
				titleText.selectable = false;
	        	titleText.autoSize = TextFieldAutoSize.LEFT;
				titleText.defaultTextFormat = new TextFormat("微软雅黑", fontSize, 0x000000);
				titleText.text = text;
				titleText.x = (this.width - titleText.width) / 2;
				titleText.y = (this.height - titleText.height) / 2;
				addChild(titleText);
			}
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			disableFlag = false;
		}
		
		public function disable():void
		{
			this.filters = [new ColorMatrixFilter(BFont.blackWhiteFilter)];
			disableFlag = true;
		}
		
		public function enable():void
		{
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			this.filters = null;
			disableFlag = false;
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			if (disableFlag)
				return;
			Mouse.cursor = "button";
			this.filters = [new ColorMatrixFilter(
				[1,0,0,0,15,
			     0,1,0,0,15,
			     0,0,1,0,15,
			     0,0,0,1,0 ])];
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			if (disableFlag)
				return;
			this.filters = null;
		}
	}
}